/**
 * DOM 操作封装
 */
const https = require('../utils/https');

// const { call } = require("file-loader");

function ready(callback) {
    addEventListener("DOMContentLoaded", callback)
}
// 获取单个元素节点
function getEle(selector) {
    return document.querySelector(selector)
}
// 获取多个元素节点
function getEles(selector) {
    return document.querySelectorAll(selector)
}
// 绑定事件

function addEvent(obj, type = 'click', callback) {
    getEle(obj).addEventListener(type, callback)
}

function creatfoot(obj) {
    document.body.innerHTML += `
    <footer>
        <ul>
            <li>
                <a href="./home.html" class="${obj == 'home'?'on':''}">
                    <span class="iconfont icon-home"></span>
                    <i>首页</i>
                </a>
            </li>
            <li>
                <a href="./sport.html" class="${obj == 'sport'?'on':''}">
                    <span class="iconfont icon-sport"></span>
                    <i>运动</i>
                </a>
            </li>
            <li>
                <a href="./circle.html" class="${obj == 'circle'?'on':''}">
                    <span class="iconfont icon-circle"></span>
                    <i>圈子</i>
                </a>
            </li>
            <li>
                <a href="./mine.html" class="${obj == 'mine'?'on':''}">
                    <span class="iconfont icon-mine"></span>
                    <i>我的</i>
                </a>
            </li>
        </ul>
    </footer>
    `
}

//处理小于10
function handleData(val) {
    return val < 10 ? "0" + val : val
}

// 图像上传
function upLoadPic(url, key, file, callback) {
    let fd = new FormData;
    fd.append(key, file);
    https.post(url, fd, callback)
}

module.exports = {
    ready,
    getEle,
    getEles,
    addEvent,
    creatfoot,
    upLoadPic,
    handleData
}